package com.lanou.wuyou.gift.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.wuyou.gift.R;
import com.lanou.wuyou.gift.bean.HomeFirstBean;
import com.squareup.picasso.Picasso;

/**
 * Created by dllo on 17/3/16.
 */

public class HomeFirstAdapter extends BaseAdapter {
    private Context context;
    private HomeFirstBean homeFirstBean;

    public void setHomeFirstBean(HomeFirstBean homeFirstBean) {
        this.homeFirstBean = homeFirstBean;
        notifyDataSetChanged();
    }

    public HomeFirstAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        if (homeFirstBean == null) {
            return 0;
        }
        if (homeFirstBean.getData() == null) {
            return 0;
        }
        return homeFirstBean.getData().getItems() != null && homeFirstBean.getData().getItems().size() > 0 ? homeFirstBean.getData().getItems().size() : 0;
    }
    @Override
    public Object getItem(int position) {
        return homeFirstBean.getData().getItems().get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {

        if (position == 2) {
            return 4;
        }
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 5;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        AdvertisementHolder advertisementHolder = null;
        ForthHolder forthHolder = null;
        if (convertView == null) {
            switch (getItemViewType(position)){
                case 4:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_home_first_advertisement,parent,false);
                    advertisementHolder = new AdvertisementHolder(convertView);
                    convertView.setTag(advertisementHolder);
                    break;
                case 0:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_first_home, parent, false);
                    forthHolder = new ForthHolder(convertView);
                    convertView.setTag(forthHolder);
                    break;
            }

        } else {
            switch (getItemViewType(position)) {
                case 4:
                    advertisementHolder = (AdvertisementHolder) convertView.getTag();
                    break;
                case 0:
                    forthHolder = (ForthHolder) convertView.getTag();
                    break;
            }
        }
        if (homeFirstBean != null) {
            switch (getItemViewType(position)){
                case 4:
                    HomeFirstBean.DataBean.ItemsBean itemsBeanXX1 = homeFirstBean.getData().getItems().get(4);
                    if (itemsBeanXX1 != null){
                        Picasso.with(context).load(itemsBeanXX1.getImage_url()).into(advertisementHolder.imageView);
                        advertisementHolder.imageView.setAdjustViewBounds(true);
                    }
                    break;
                case 0:
                    HomeFirstBean.DataBean.ItemsBean itemsBeanXX = homeFirstBean.getData().getItems().get(position);
                    if (itemsBeanXX != null) {
                        Picasso.with(context).load(itemsBeanXX.getCover_image_url()).into(forthHolder.bannerIv);
                    }
                    HomeFirstBean.DataBean.ItemsBean.ColumnBean columnBean = homeFirstBean.getData().getItems().get(position).getColumn();
                    if (columnBean != null){
                        forthHolder.titleCTv.setText(columnBean.getTitle());
                    }
                    if (homeFirstBean.getData().getItems().get(position).getAuthor() != null) {
                        HomeFirstBean.DataBean.ItemsBean.AuthorBean authorBean = homeFirstBean.getData().getItems().get(position).getAuthor();
                        if (authorBean != null) {
                            Picasso.with(context).load(authorBean.getAvatar_url()).into(forthHolder.avatarIv);
                            forthHolder.nicknameTv.setText(authorBean.getNickname());
                            forthHolder.introductionTv.setText(authorBean.getIntroduction());
                        }
                    }
                    if (homeFirstBean.getData().getItems().get(position).getIntroduction() != null) {
                        String introduction = homeFirstBean.getData().getItems().get(position).getIntroduction();
                        if (introduction != null) {
                            forthHolder.introductionCTv.setText(introduction);
                        }
                    }
                    if (homeFirstBean.getData().getItems().get(position).getLikes_count() != 0) {
                        int likes = homeFirstBean.getData().getItems().get(position).getLikes_count();
                        if (likes != 0) {
                            forthHolder.likesTv.setText(likes + "");
                        }
                        if (homeFirstBean.getData().getItems().get(position).getTitle() != null) {
                            String title = homeFirstBean.getData().getItems().get(position).getTitle();
                            if (title != null) {
                                forthHolder.titleTv.setText(title);
                            }
                        }
                    }
                    break;
                }
            }
            return convertView;
    }
    class AdvertisementHolder{
        ImageView imageView;
        public AdvertisementHolder(View itemView){
            imageView = (ImageView) itemView.findViewById(R.id.home_first_advertisement);
        }
    }
    class ForthHolder {
        private TextView nicknameTv, introductionTv, titleTv, introductionCTv, titleCTv, likesTv;
        private ImageView avatarIv, bannerIv;

        public ForthHolder(View itemView) {
            nicknameTv = (TextView) itemView.findViewById(R.id.nickname_tv_first);
            introductionTv = (TextView) itemView.findViewById(R.id.introduction_tv_first);
            titleTv = (TextView) itemView.findViewById(R.id.title_tv_first);
            introductionCTv = (TextView) itemView.findViewById(R.id.introduction_content_first);
            titleCTv = (TextView) itemView.findViewById(R.id.title_column_first);
            likesTv = (TextView) itemView.findViewById(R.id.likes_tv_first);
            avatarIv = (ImageView) itemView.findViewById(R.id.avatar_iv_first);
            bannerIv = (ImageView) itemView.findViewById(R.id.banner_iv_first);
        }
    }
}
